package com.info.zhiduoduo.repository.mapper.company;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.company.CompanyZhaoshangSubLedgerRechargeDto;
import com.info.zhiduoduo.repository.entity.company.CompanyZhaoshangSubLedgerRecharge;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

public interface CompanyZhaoshangSubLedgerRechargeMapper extends BaseMapper<CompanyZhaoshangSubLedgerRecharge> {

	@Select(" select COUNT(1) from zdd_company_zhaoshang_sub_ledger_recharge where ledger_id = #{ledgerId} and state in (1, 2)")
	int existsProcessingOrder(int ledgerId);

	@Select(" select * from zdd_company_zhaoshang_sub_ledger_recharge where state = 2 ")
	List<CompanyZhaoshangSubLedgerRecharge> listAllProcessing();

	@Select(" select czslr.*,pm.name as merchantName,czsl.ledger_name from zdd_company_zhaoshang_sub_ledger_recharge czslr " +
			" join zdd_company_zhaoshang_sub_ledger czsl on czsl.id = czslr.ledger_id " +
			" join zdd_payment_merchant pm on pm.id = czsl.merchant_id " +
			" LEFT JOIN zdd_company_account ca ON czslr.compid = ca.compid " +
			" where if(#{compid} = 0, 1=1, czslr.compid = #{compid}) " +
			" and czsl.ledger_name like '%${compname}%' " +
			" and if(#{merchantId} = 0, 1=1, czslr.merchant_id = #{merchantId}) " +
			" and CONCAT(czslr.order_no, czslr.payer_trxnbr, czslr.payee_trxnbr) like '%${orderNo}%' " +
			" and czslr.createtime between #{from} and #{to} " +
			" order by czslr.id desc")
	List<CompanyZhaoshangSubLedgerRechargeDto> listByMerchantIdAndName(int compid, String compname, int merchantId, String orderNo,
	                                                                   Date from, Date to);
	@Select(" SELECT " +
			" 	*  " +
			" FROM " +
			" 	zdd_company_zhaoshang_sub_ledger_recharge " +
			" WHERE compid = #{compid} " +
			" AND state = 3 " +
			" ORDER BY id DESC " +
			" 	LIMIT 1 ")
	CompanyZhaoshangSubLedgerRecharge ledgerRecharge(@Param("compid")int compid );
}
